#!/bin/bash
#
# Copyright (c) 2023, 2024 Eurotech and/or its affiliates and others
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#  Eurotech
#

INSTALL_DIR=/opt/eclipse
WD_TMP_FILE=/tmp/watchdog
REFRESH_TIME=5
TIMEOUT_TIME=300
SYSTEMD_WATCHDOG_CONFIG="[Manager]
RuntimeWatchdogSec=0
RebootWatchdogSec=0
KExecWatchdogSec=0
"

##############################################
# UTILITY FUNCTIONS
##############################################
# Run preInstall function and start watchdog if needed
function runPreInstall {
    if [ -f "${WD_TMP_FILE}" ]; then
        WATCHDOG_DEVICE=`cat ${WD_TMP_FILE}`
        preInstall &
        PID=$!
        START=$(date +%s)
        
        while [ -d "/proc/$PID" ]; do
            echo w > ${WATCHDOG_DEVICE}
            DELTA=$(($(date +%s) - $START))
            if [ "$DELTA" -ge "$TIMEOUT_TIME" ]; then
                echo "The installation process is not responding. It'll be stopped."
                kill -9 $PID >> /dev/null 2>&1
            fi
            sleep $REFRESH_TIME
        done
        stopWatchdog
    else
        preInstall &
        PID=$!
        START=$(date +%s)
        
        while [ -d "/proc/$PID" ]; do
            DELTA=$(($(date +%s) - $START))
            if [ "$DELTA" -ge "$TIMEOUT_TIME" ]; then
                echo "The installation process is not responding. It'll be stopped."
                kill -9 $PID >> /dev/null 2>&1
            fi
            sleep $REFRESH_TIME
        done
    fi
}

# Deactivate watchdog device if possible
function stopWatchdog {
    if [ -n "${WATCHDOG_DEVICE}" ]; then
        echo V > ${WATCHDOG_DEVICE}
    fi
}

function disable_systemd_watchdog() {
    # disable systemd watchdog
    # https://manpages.debian.org/testing/systemd/systemd-system.conf.5.en.html
    #
    # Order of application of conf files:
    # 1. /usr/lib/systemd/system.conf.d/
    # 2. /usr/local/lib/systemd/system.conf.d/
    # 3. /etc/systemd/system.conf.d/
    # docs suggest to use 10-40 priority for drop-ins in /usr/, and 50-90 for /etc/
    # we use zz since some OSs do not respect that convention (raspbian, ubuntu jammy)
    #
    path=""
    if [ -d /usr/lib/systemd/system.conf.d/ ]; then
        path="/usr/lib/systemd/system.conf.d"
    elif [ -d /usr/local/lib/systemd/system.conf.d/ ]; then
        path="/usr/local/lib/systemd/system.conf.d"
    elif [ -d /etc/systemd/system.conf.d/ ]; then
        path="/etc/systemd/system.conf.d"        
    fi

    if [ ! -z "${path}" ]; then
        echo "Installing $path/zz-kura-disable-watchdog.conf"
        echo -e "${SYSTEMD_WATCHDOG_CONFIG}" > ${path}/zz-kura-disable-watchdog.conf
        chmod 644 ${path}/zz-kura-disable-watchdog.conf
        systemctl daemon-reload
    else
        echo "No systemd drop-in directory found, watchdog not disabled"
    fi
}

# Pre-install script
function preInstall {
    #clean up old installation if present
    rm -fr /opt/eclipse/kura* >> /tmp/kura_install.log 2>&1
    rm -fr ${INSTALL_DIR}/kura* >> /tmp/kura_install.log 2>&1
    rm -fr /tmp/.kura/ >> /tmp/kura_install.log 2>&1
    rm /var/log/kura.log >> /tmp/kura_install.log 2>&1
    rm -f kura-*.zip >> /tmp/kura_install.log 2>&1
    rm -f kura_*.zip >> /tmp/kura_install.log 2>&1
    echo ""
}
##############################################
# END UTILITY FUNCTIONS
##############################################

##############################################
# PRE-INSTALL SCRIPT
##############################################
PIDS=`pgrep java`

echo ""
echo "Installing Kura..."
echo "Installing Kura..." > /tmp/kura_install.log 2>&1

#Kill JVM and monit for installation
if [ -f "/var/run/kura.pid" ] ; then
  KURA_PID=`cat /var/run/kura.pid`

  for pid in "${PIDS[@]}"
  do
    if [ "$KURA_PID" == "$pid" ] ; then
      `kill $pid` >> /tmp/kura_install.log 2>&1
    fi
  done
fi

disable_systemd_watchdog
runPreInstall
##############################################
# END PRE-INSTALL SCRIPT
##############################################

